#include "GameNetClient.h"
#include "KMessage.h"
#include "helper/log.h"

#define RECV_LEN 4096

namespace KBEngineClient
{

GameNetClient::GameNetClient()
{
    heartBeatTime = 0;
}

//void GameNetClient::handlMessage(Message& msg)
//{
//  uint16 messageID = msg.getOpcode();
//  switch(messageID)
//  {
//  case msg_player_role_list_s2c:
//      {
//          //LoginController::getInstance().recRoleListS2C(msg);
//          break;
//      }
//  default:
//      break;
//  }
//}

void GameNetClient::tick(float dt)
{
    NetClient::tick(dt);
    //return;//for test.
    if (sock && isConnect)
    {
        handleHeartBeat(dt);
    }
}

void GameNetClient::noticeSocketDisconnect(int len, int sockErr)
{
    xpy::logger::info("socket disconnet!!!!!!!!!, len: {}, error: {}", len, sockErr);
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    switch (sockErr)
    {
    case ETIMEDOUT:
		xpy::logger::info("Connection timed out");
        break;
    case ECONNREFUSED:
		xpy::logger::info("Connection refused");
        break;
    case EHOSTDOWN:
		xpy::logger::info("Host is down");
        break;
    case EHOSTUNREACH:
		xpy::logger::info("No route to host");
        break;
    }
#endif
    xpy::logger::info("socket disconnet!", "");
}

void GameNetClient::reConnectBtnCallback(void *result, void *bindData)
{
}

void GameNetClient::handleHeartBeat(float dt)
{
    heartBeatTime += dt;
    if (heartBeatTime > HEARTBEAT_INTERVAL)
    {
        //KMessage::

        /*  Message msg;
        msg.setOpcode(msg_heartbeat_c2s);
        int beatTime = TimeUtil::getInstance().currentTimestamp1();
        msg << beatTime;
        sendMessage(msg);*/
        KMessage::sendActiveAck();
        heartBeatTime = 0;
    }
}

}
